package com.fn.useries;


import android.app.Activity;
import android.app.Application;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothSocket;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v4.app.Fragment;

import com.M10.BE.R;
import com.fn.useries.reader.server.ReaderHelper;
import com.fn.useries.utils.MusicPlayer;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cookie.CookieJarImpl;
import com.lzy.okgo.cookie.store.MemoryCookieStore;
import com.lzy.okgo.interceptor.HttpLoggingInterceptor;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreater;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreater;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;

import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import okhttp3.OkHttpClient;

public class UHFApplication extends Application {

    private Socket mTcpSocket = null;
    private BluetoothSocket mBtSocket = null;
    public static Context applicationContext;
    private List<Activity> activities = new ArrayList<Activity>();
    private List<Fragment> fragments = new ArrayList<Fragment>();
    //    public static String BASE_HEAD = "http://192.168.1.6:8080/cdpfas/app/";
    public static String BASE_HEAD = "http://w1045632741w.xicp.cn:11032/cdpfas/";

    @Override
    public void onCreate() {

        super.onCreate();
        applicationContext = getApplicationContext();
        CrashHandler crashHandler = CrashHandler.getInstance();
//        crashHandler.init(applicationContext);
        MusicPlayer.getInstance();
        try {
            //实例化ReaderHelper并setContext
            ReaderHelper.setContext(applicationContext);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

		/*CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(getApplicationContext());*/

        //OKGO的初始化
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("OkGo");
        //log打印级别，决定了log显示的详细程度
        loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);
        //log颜色级别，决定了log在控制台显示的颜色
        loggingInterceptor.setColorLevel(Level.INFO);
        builder.addInterceptor(loggingInterceptor);
        builder.cookieJar(new CookieJarImpl(new MemoryCookieStore()));
        builder.connectTimeout(5, TimeUnit.SECONDS);
        builder.readTimeout(5, TimeUnit.SECONDS);
        builder.writeTimeout(5, TimeUnit.SECONDS);
        OkGo.getInstance().init(this).setRetryCount(2)
                .setOkHttpClient(builder.build());
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                //全局设置主题颜色
                layout.setPrimaryColorsId(R.color.colorPrimary, android.R.color.white);
                //指定为经典Header，默认是 贝塞尔雷达Header
                return new ClassicsHeader(getApplicationContext());
            }
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreater(new DefaultRefreshFooterCreater() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                return new ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate);
            }
        });
//        customSQLiteOpenHelper = new CustomSQLiteOpenHelper(this);
//        customSQLiteOpenHelper.getWritableDatabase();
    }

    public void addActivity(Activity activity) {
        activities.add(activity);
    }

    public void addFragment(Fragment fragment) {
        fragments.add(fragment);
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        for (Activity activity : activities) {
            try {
                activity.finish();
            } catch (Exception e) {

            }
        }
        for (Fragment fragment : fragments) {
            try {
                fragment.getActivity().finish();
            } catch (Exception e) {

            }
        }

        try {
            if (mTcpSocket != null) mTcpSocket.close();
            if (mBtSocket != null) mBtSocket.close();
        } catch (IOException e) {
        }

        mTcpSocket = null;
        mBtSocket = null;

        if (BluetoothAdapter.getDefaultAdapter() != null)
            BluetoothAdapter.getDefaultAdapter().disable();
        System.exit(0);
    }

    ;

    public void setTcpSocket(Socket socket) {
        this.mTcpSocket = socket;
    }

    public void setBtSocket(BluetoothSocket socket) {
        this.mBtSocket = socket;
    }

    static public void saveBeeperState(int state) {
        SharedPreferences spf = applicationContext.getSharedPreferences("setting", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = spf.edit();
        editor.putInt("_state", state);
        editor.commit();
    }

    static int _SoftSound = 2;

    static public int appGetSoftSound() {
        if (_SoftSound == 2)
            _SoftSound = getSoftSound();
        return _SoftSound;
    }

    static public int getVeeperState() {
        SharedPreferences spf = applicationContext.getSharedPreferences("setting", Activity.MODE_PRIVATE);
        //SharedPreferences.Editor editor = spf.edit();
        int state = spf.getInt("_state", 0);
        return state;
    }

    static public void saveSoftSound(int state) {
        _SoftSound = state;
        SharedPreferences spf = applicationContext.getSharedPreferences("setting", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = spf.edit();
        editor.putInt("_software_sound", state);
        editor.commit();
    }

    static public int getSoftSound() {
        SharedPreferences spf = applicationContext.getSharedPreferences("setting", Activity.MODE_PRIVATE);
        //SharedPreferences.Editor editor = spf.edit();
        int state = spf.getInt("_software_sound", 1);
        return state;
    }

    static public void saveSessionState(int state) {
        SharedPreferences spf = applicationContext.getSharedPreferences("setting", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = spf.edit();
        editor.putInt("_session", state);
        editor.commit();
    }

    static public int getSessionState() {
        SharedPreferences spf = applicationContext.getSharedPreferences("setting", Activity.MODE_PRIVATE);
        //SharedPreferences.Editor editor = spf.edit();
        int state = spf.getInt("_session", 0);
        return state;
    }

    static public void saveFlagState(int state) {
        SharedPreferences spf = applicationContext.getSharedPreferences("setting", Activity.MODE_PRIVATE);
        SharedPreferences.Editor editor = spf.edit();
        editor.putInt("_flag", state);
        editor.commit();
    }

    static public int getFlagState() {
        SharedPreferences spf = applicationContext.getSharedPreferences("setting", Activity.MODE_PRIVATE);
        //SharedPreferences.Editor editor = spf.edit();
        int state = spf.getInt("_flag", 0);
        return state;
    }
}
